package com.ruoyi.game.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.game.constants.Constants;
import com.ruoyi.game.domain.GameCharge;
import com.ruoyi.game.domain.GameInterface;
import com.ruoyi.game.service.IGameInterfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.game.domain.Game;
import com.ruoyi.game.service.IGameService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 游戏列表Controller
 *
 * @author qidian
 * @date 2022-05-05
 */
@RestController
@RequestMapping("/game")
public class GameController extends BaseController
{
    @Autowired
    private IGameService gameService;

    @Autowired
    private IGameInterfaceService gameInterfaceService;


    /**
     * 查询游戏列表列表
     */
//    @RequiresPermissions("game:game:list")
    @GetMapping("/list")
    public TableDataInfo list(Game game)
    {
        startPage();
        List<Game> list = gameService.selectGameList(game);
        return getDataTable(list);
    }

    /**
     * 导出游戏列表列表
     */
    @RequiresPermissions("game:game:export")
    @Log(title = "游戏列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Game game)
    {
        List<Game> list = gameService.selectGameList(game);
        ExcelUtil<Game> util = new ExcelUtil<Game>(Game.class);
        util.exportExcel(response, list, "游戏列表数据");
    }

    /**
     * 获取游戏列表详细信息
     */
    @RequiresPermissions("game:game:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gameService.selectGameById(id));
    }

    /**
     * 新增游戏列表
     */
    @RequiresPermissions("game:game:add")
    @Log(title = "游戏列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Game game)
    {
        game.setCreateBy("admin");
        return toAjax(gameService.insertGame(game));
    }

    /**
     * 修改游戏列表
     */
    @RequiresPermissions("game:game:edit")
    @Log(title = "游戏列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Game game)
    {
        game.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(gameService.updateGame(game));
    }

    /**
     * 删除游戏列表
     */
    @RequiresPermissions("game:game:remove")
    @Log(title = "游戏列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gameService.deleteGameByIds(ids));
    }

    /**
     * 修改游戏状态
     */
    @RequiresPermissions("game:game:changeStatus")
    @Log(title = "游戏列表", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Game game)
    {
        game.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(gameService.updateGameStatus(game));
    }

    /**
     * 充值
     */
    @Log(title = "游戏充值", businessType = BusinessType.RECHARGE)
    @PostMapping("/recharge")
    public AjaxResult recharge(@RequestBody GameCharge gameCharge){

        //权限校验
      //通过account+type+gameID查询是否授权

        //查询接口字段
        GameInterface gameInterface = gameInterfaceService.selectGameInterfaceByGameId(gameCharge.getGameId());
        Map map = new HashMap<String, Object>();
        map.put(gameInterface.getAccount(), gameCharge.getAccount());
        Map parse = (Map) JSONUtil.parse(gameInterface.getOtherParams());
        if(parse!=null) {
            map.putAll(parse);
        }
        if (Constants.CHARGE.equals(gameCharge.getType())) {
            //充值
            map.put(gameInterface.getChargeType(), gameInterface.getCharge());
            map.put(gameInterface.getChargeItem(), gameCharge.getChargeItem());
            map.put(gameInterface.getChargenum(), gameCharge.getNum());

        }else if (Constants.MAIL.equals(gameCharge.getType())) {
            //物品
            map.put(gameInterface.getChargeType(), gameInterface.getMail());
            map.put(gameInterface.getMailItem(), gameCharge.getMailItem());
            map.put(gameInterface.getMailnum(), gameCharge.getMailNum());
        }

        //遍历map
        for (Object key : map.keySet()) {
            System.out.println("key= " + key + " and value= " + map.get(key));
        }
        return AjaxResult.success(HttpUtil.post(gameInterface.getInterfaceAddr(), map));
    }

}
